package com.iqiyi.pps.epg.web.webapp.action.auth;


import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.exception.ServiceException;
import com.iqiyi.pps.epg.core.model.auth.Authority;
import com.iqiyi.pps.epg.core.model.auth.Resource;
import com.iqiyi.pps.epg.core.service.auth.AuthorityManager;
import com.iqiyi.pps.epg.web.utils.Struts2Utils;
import com.iqiyi.pps.epg.web.webapp.action.BaseAction;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
  Action. 
*/

@SuppressWarnings("serial")
@Namespace("/auth/pop")
@Scope("prototype")
@Results( { @Result(name = BaseAction.RELOAD, location = "pop-authority.action", type = "redirect") })
public class PopAuthorityAction extends BaseAction<Authority> {
    
	/*Manager*/
	@Autowired
	private AuthorityManager authorityManager;
	
	/*Basic Property*/
    private Authority authority;
	private Long id;
	private Page<Authority> page = new  Page<Authority>(15);
	
	// 该权限被授权可以访问的资源
	private List<Resource> resources;
	
	
	/*Model: for Prepare & Model iservice*/
	
	public Authority getModel() {
		return authority;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			authority = authorityManager.getAuthority(id);
		} else {
			authority = new Authority();
		}
	}
	
	
	/*List */
	@Override
    @Action(value = "list")
	public String list() throws Exception {
		HttpServletRequest request = Struts2Utils.getRequest();
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);

		page = authorityManager.searchAuthority(page, filters);
		return SUCCESS;
	}
	
	/*Save*/
	@Override
	public String save() throws Exception {
		authorityManager.saveAuthority(authority);
		addActionMessage("保存成功！");
		return RELOAD;
	}

	/*delete*/
	@Override
	public String delete() throws Exception {
		try {
			authorityManager.deleteAuthority(id);
			addActionMessage("删除成功！");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage(e.getMessage());
		}
		return RELOAD;
	}
	
	/*input*/
	@Override
	public String input() throws Exception {
		return INPUT;
	}
	
	
	/*getter & setter*/
	public void setId(Long id) {
		this.id = id;
	}

	public Page<Authority> getPage() {
		return page;
	}

	public List<Resource> getResources() {
		return resources;
	}

	public void setResources(List<Resource> resources) {
		this.resources = resources;
	}

}


